package com.ykt.controller;

import com.jfinal.aop.Before;
import com.ykt.common.constant.ValueConstants;
import com.ykt.common.thirdapi.ys7.model.Ys7Camera;
import com.ykt.common.utils.UB;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;
import com.ykt.converter.CameraConverter;
import com.ykt.model.Camera;
import com.ykt.service.CameraService;
import com.ykt.validator.CameraValidator;
import com.ykt.vo.ResultVo;

import java.util.ArrayList;

/**
 * 拉取摄像头接口
 * @author long.hua
 * @version 1.0.0
 * @since 2016-01-09 23:06
 */
public class CameraController extends BaseController {

    private CameraService cameraService = enhance(CameraService.class.getName(), CameraService.class);

    /**
     * 查询指定ID的摄像头
     */
    public void detail(){
        Integer userId = UB.i(getPara("userId"));
        Integer cameraTableId = UB.i(getPara("cameraTableId"));

        Camera model = cameraService.findById(cameraTableId);

        Ys7Camera ys7Camera = CameraConverter.convert(model);

        this.returnJson(ResultVo.success(ys7Camera));
    }

    /**
     * 查询指定商铺的摄像头
     */
    @Before(CameraValidator.ShopCameraValidator.class)
    public void shopCameras(){
        Integer shopId = getParaToInt("shopId", -1);

        PageParam<Integer> param = super.toPageParam(shopId);
        PageData<Camera> pd = cameraService.findByShopId(param);

        PageData<Ys7Camera> pageVo = pd.convert(CameraConverter.convert(pd.getPageData()));
        returnJson(ResultVo.success(pageVo));
    }

    /**
     * 查询指定市场的摄像头
     */
    @Before(CameraValidator.MarketCameraValidator.class)
    public void marketCameras(){
        Integer marketId = getParaToInt("marketId", -1);
        int pageNo = getParaToInt("pageNo", 1);

        PageParam<Integer> param = new PageParam<>(marketId, pageNo, ValueConstants.PAGE_SIZE);
        PageData<Camera> pd = cameraService.findByMarketId(param);
        if(pd.getPageData() == null){
            pd.setPageData(new ArrayList<Camera>());
        }
        PageData<Ys7Camera> pdVO = pd.convert(CameraConverter.convert(pd.getPageData()));
        returnJson(ResultVo.success(pdVO));
    }
}
